﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using Polaris.Common;
using Polaris.Common.FilterAttribute;
using Polaris.Domain.Interfaces.Admin;
using Polaris.Domain.Model.Dto.Permissions.Request;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace Polaris.WebApi.Controllers.Admin
{
    /// <summary>
    /// 权限控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PermissionsController : BaseController
    {
        private readonly IPermissions _permissions;

        public PermissionsController(IPermissions permissions)
        {
            _permissions = permissions;
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="create"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> CreateRole(CreateRoleInfoRequest create)
        {
            await _permissions.CreateRole(create, null);
            return SUCCESS("添加成功");
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeleteRoleAsync(string roleId)
        {
            await _permissions.DeleteRoleAsync(roleId, null);
            return SUCCESS("删除成功");
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetRoleList()
        {
            var result = await _permissions.GetRoleList();
            return SUCCESS(result);
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetRoleAllList()
        {
            var result = await _permissions.GetRoleAllList();
            return SUCCESS(result);
        }

        /// <summary>
        /// 授权菜单给角色
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GrantMenuToRole(AllocationMenuRequest request)
        {
            await _permissions.GrantMenuToRole(request, null);
            return SUCCESS("授权成功");
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="update"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateRole(UpdateRoleInfoRequest update)
        {
            await _permissions.UpdateRole(update, null);
            return SUCCESS("修改成功");
        }

        /// <summary>
        /// 根据角色id获取授权菜单id
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetMenuIdsById(string roleId)
        {
            var result = await _permissions.GetMenuIdsById(roleId);
            return SUCCESS(result);
        }

        /// <summary>
        /// 获取登录对象的菜单权限列表
        /// </summary>
        /// <returns></returns>
        [CustomAuthorize(IsAuthorize = true)]
        [HttpGet]
        public async Task<IActionResult> GetUserMenuList()
        {
            var userId = HttpContext.GetUserId();
            var result = await _permissions.GetUserMenuList(Guid.Parse(userId));
            return SUCCESS(result);
        }
    }
}
